package tang.server;

import java.net.InetSocketAddress;

/**
 * @author Tang
 * @classname QuorumPeerMain
 * @description [  ]
 * @date 2021/3/7 10:50
 */
public class QuorumPeerMain {

    private final String host = "127.0.0.1";
    private final Integer port = 9000;
    private boolean isCluster = true;

    public static void main(String[] args) throws Exception {
        QuorumPeerMain quorumPeerMain = new QuorumPeerMain();
        quorumPeerMain.initializeAndRun(args);
    }

    private void initializeAndRun(String[] args) throws Exception {

        if (isCluster) {
            // 1.解析zk配置文件
            // 2.运行
            runFromConfig();
        } else {
            ZKServerMain.main(args);
        }

    }

    private void runFromConfig() throws Exception {
        QuorumPeer quorumPeer = getQuorumPeer();
        NioServerCnxnFactory nioServerCnxnFactory = new NioServerCnxnFactory();
        nioServerCnxnFactory.configure(new InetSocketAddress(host,port));

        // 设置各种属性
        quorumPeer.setSetverCnxnFactory(nioServerCnxnFactory);

        quorumPeer.init();
        quorumPeer.start();
    }

    public QuorumPeer getQuorumPeer() {
        return new QuorumPeer();
    }

}